package com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.constants.PlatformConstants;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.PlatformDateUtils;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.sjcj.common.excel.handler.ActiveFaultVerifyBaseHandler;
import com.css.zfzx.sjcj.common.utils.EasyPoiExcelUtil;
import com.css.zfzx.sjcj.common.utils.UpdateUtil;
import com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.repository.HddcA6StrongseihasfractbltNativeRepository;
import com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.repository.HddcA6StrongseihasfractbltRepository;
import com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.repository.entity.HddcA6StrongseihasfractbltEntity;
import com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.service.HddcA6StrongseihasfractbltService;
import com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.viewobjects.HddcA6StrongseihasfractbltPageVO;
import com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.viewobjects.HddcA6StrongseihasfractbltQueryParams;
import com.css.zfzx.sjcj.modules.hddcA6StrongSeiHasFractBlt.viewobjects.HddcA6StrongseihasfractbltVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zyb
 * @date 2020-11-28
 */
@Service
public class HddcA6StrongseihasfractbltServiceImpl implements HddcA6StrongseihasfractbltService {

    private final HddcA6StrongseihasfractbltRepository hddcA6StrongseihasfractbltRepository;
    private final HddcA6StrongseihasfractbltNativeRepository hddcA6StrongseihasfractbltNativeRepository;
    private final ActiveFaultVerifyBaseHandler activeFaultVerifyBaseHandler;

    @Autowired
    public HddcA6StrongseihasfractbltServiceImpl(HddcA6StrongseihasfractbltRepository hddcA6StrongseihasfractbltRepository,
                                                 HddcA6StrongseihasfractbltNativeRepository hddcA6StrongseihasfractbltNativeRepository,
                                                 ActiveFaultVerifyBaseHandler activeFaultVerifyBaseHandler) {
        this.hddcA6StrongseihasfractbltRepository = hddcA6StrongseihasfractbltRepository;
        this.hddcA6StrongseihasfractbltNativeRepository = hddcA6StrongseihasfractbltNativeRepository;
        this.activeFaultVerifyBaseHandler = activeFaultVerifyBaseHandler;
    }

    @Override
    public JSONObject queryHddcA6Strongseihasfractblts(HddcA6StrongseihasfractbltQueryParams queryParams, int curPage, int pageSize,String sort,String order) {
        Page<HddcA6StrongseihasfractbltPageVO> hddcA6StrongseihasfractbltPage = this.hddcA6StrongseihasfractbltNativeRepository.queryHddcA6Strongseihasfractblts(queryParams, curPage, pageSize, sort, order);
        return PlatformPageUtils.formatPageData(hddcA6StrongseihasfractbltPage);
    }


    @Override
    public HddcA6StrongseihasfractbltEntity getHddcA6Strongseihasfractblt(String id) {
        return this.hddcA6StrongseihasfractbltRepository.findById(id).orElse(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcA6StrongseihasfractbltEntity saveHddcA6Strongseihasfractblt(HddcA6StrongseihasfractbltEntity hddcA6Strongseihasfractblt) {
        String uuid = UUIDGenerator.getUUID();
        hddcA6Strongseihasfractblt.setUuid(uuid);
        hddcA6Strongseihasfractblt.setCreateUser(PlatformSessionUtils.getUserId());
        Timestamp now = PlatformDateUtils.getCurrentTimestamp();
        hddcA6Strongseihasfractblt.setCreateTime(now);
        hddcA6Strongseihasfractblt.setUpdateTime(now);
        hddcA6Strongseihasfractblt.setIsValid(PlatformConstants.Y);
        hddcA6Strongseihasfractblt.setQualityinspectionStatus(PlatformConstants.N);
        hddcA6Strongseihasfractblt.setExtends10(PlatformConstants.Y);
        this.hddcA6StrongseihasfractbltRepository.save(hddcA6Strongseihasfractblt);
        return hddcA6Strongseihasfractblt;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcA6StrongseihasfractbltEntity updateHddcA6Strongseihasfractblt(HddcA6StrongseihasfractbltEntity hddcA6Strongseihasfractblt) {
        HddcA6StrongseihasfractbltEntity entity = hddcA6StrongseihasfractbltRepository.findById(hddcA6Strongseihasfractblt.getUuid()).get();
        UpdateUtil.copyNullProperties(entity,hddcA6Strongseihasfractblt);
        hddcA6Strongseihasfractblt.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcA6Strongseihasfractblt.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcA6StrongseihasfractbltRepository.save(hddcA6Strongseihasfractblt);
        return hddcA6Strongseihasfractblt;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcA6Strongseihasfractblts(List<String> ids) {
        List<HddcA6StrongseihasfractbltEntity> hddcA6StrongseihasfractbltList = this.hddcA6StrongseihasfractbltRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcA6StrongseihasfractbltList) && hddcA6StrongseihasfractbltList.size() > 0) {
            for(HddcA6StrongseihasfractbltEntity hddcA6Strongseihasfractblt : hddcA6StrongseihasfractbltList) {
//                this.hddcA6StrongseihasfractbltRepository.delete(hddcA6Strongseihasfractblt);
                hddcA6Strongseihasfractblt.setIsValid("0");
                hddcA6Strongseihasfractblt.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcA6Strongseihasfractblt.setUpdateTime(new Date());
                this.hddcA6StrongseihasfractbltRepository.save(hddcA6Strongseihasfractblt);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        return PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
    }

    @Override
    public void exportFile(HddcA6StrongseihasfractbltQueryParams queryParams, HttpServletResponse response) {
        List<HddcA6StrongseihasfractbltEntity> yhDisasterEntities = hddcA6StrongseihasfractbltNativeRepository.exportYhDisasters(queryParams);
        List<HddcA6StrongseihasfractbltVO> list=new ArrayList<>();
        for (HddcA6StrongseihasfractbltEntity entity:yhDisasterEntities) {
            HddcA6StrongseihasfractbltVO yhDisasterVO=new HddcA6StrongseihasfractbltVO();
            BeanUtils.copyProperties(entity,yhDisasterVO);
            list.add(yhDisasterVO);
        }
        EasyPoiExcelUtil.exportExcel(list,"强震与地表破裂关联表","强震与地表破裂关联表",HddcA6StrongseihasfractbltVO.class,"强震与地表破裂关联表.xls",response);
    }

    @Override
    public String exportExcel(MultipartFile file, HttpServletResponse response) {
        try {
            // 返回的消息
            StringBuilder returnMsg = new StringBuilder();
            // 导入的参数信息
            ImportParams params = new ImportParams();
            // 设置简析的第一行
            params.setHeadRows(1);
            // 是否需要校验
            params.setNeedVerify(true);
            params.setVerifyHandler(activeFaultVerifyBaseHandler);
            // 获取到Excel数据
            ExcelImportResult<HddcA6StrongseihasfractbltVO> result = ExcelImportUtil.importExcelMore(file.getInputStream(), HddcA6StrongseihasfractbltVO.class, params);
            if (result.isVerifyFail()) {
                // 校验返回失败行信息
                for (HddcA6StrongseihasfractbltVO yhDisasterVO : result.getFailList()) {
                    returnMsg.append("第").append(yhDisasterVO.getRowNum()).append("行").append(yhDisasterVO.getErrorMsg()).append("<br/>");
                }
                if (returnMsg.length() > 0)
                    return returnMsg.toString();
            }
            List<HddcA6StrongseihasfractbltVO> list = result.getList();
            //保存
            saveDisasterList(list);
            return returnMsg.append("成功导入").append(result.getList().size()).toString();
        } catch (Exception e) {
            return "导入失败，请检查数据正确性";
        }
    }
    /**
     * 批量存入库表并且返回错误数量
     *
     */
    public void saveDisasterList(List<HddcA6StrongseihasfractbltVO> list) {
        for (HddcA6StrongseihasfractbltVO hddcA6StrongseihasfractbltVO : list) {
            HddcA6StrongseihasfractbltEntity yhDisasterEntity = new HddcA6StrongseihasfractbltEntity();
            // 将VO数据映射拷贝到库表中
            BeanUtils.copyProperties(hddcA6StrongseihasfractbltVO, yhDisasterEntity);
            saveHddcA6Strongseihasfractblt(yhDisasterEntity);
        }
    }

    /**
     * 逻辑删除-根据项目id删除数据
     */
    @Override
    public void deleteByProjectId(List<String> projectIds) {
        List<HddcA6StrongseihasfractbltEntity> hddcA1InvrgnhasmaterialtableList = this.hddcA6StrongseihasfractbltRepository.queryByProjectId(projectIds);
        if(!PlatformObjectUtils.isEmpty(hddcA1InvrgnhasmaterialtableList) && hddcA1InvrgnhasmaterialtableList.size() > 0) {
            for(HddcA6StrongseihasfractbltEntity hddcA1Invrgnhasmaterialtable : hddcA1InvrgnhasmaterialtableList) {
                hddcA1Invrgnhasmaterialtable.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcA1Invrgnhasmaterialtable.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                hddcA1Invrgnhasmaterialtable.setIsValid(PlatformConstants.N);
                this.hddcA6StrongseihasfractbltRepository.save(hddcA1Invrgnhasmaterialtable);
            }
        }
    }

    /**
     * 逻辑删除-根据任务id删除数据
     */
    @Override
    public void deleteByTaskId(List<String> taskId) {
        List<HddcA6StrongseihasfractbltEntity> hddcA1InvrgnhasmaterialtableList = this.hddcA6StrongseihasfractbltRepository.queryByTaskId(taskId);
        if(!PlatformObjectUtils.isEmpty(hddcA1InvrgnhasmaterialtableList) && hddcA1InvrgnhasmaterialtableList.size() > 0) {
            for(HddcA6StrongseihasfractbltEntity hddcA1Invrgnhasmaterialtable : hddcA1InvrgnhasmaterialtableList) {
                hddcA1Invrgnhasmaterialtable.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcA1Invrgnhasmaterialtable.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                hddcA1Invrgnhasmaterialtable.setIsValid(PlatformConstants.N);
                this.hddcA6StrongseihasfractbltRepository.save(hddcA1Invrgnhasmaterialtable);
            }
        }
    }

}
